VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "ShapeGenerator"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
'*************************************************************************
'  This source code is provided "as is" and as an unsupported sample of
'  a Microsoft Visual Basic application that incorporates the MapPoint 2002
'  object model and ActiveX control. It can only  be used in accordance with
'  the terms and conditions of the Microsoft MapPoint 2002 and
'  Microsoft Visual Basic End User License Agreements.
'*************************************************************************

Private Enum FILE_TYPE
    FILE_TYPE_MAPINFO_ASCII = 0
    FILE_TYPE_MAPINFO_BINARY
    FILE_TYPE_ESRI_ASCII
    FILE_TYPE_ESRI_BINARY
    FILE_TYPE_UNKNOWN
End Enum

Private m_asTextLines() As String
Private m_oObjectsToRender As Object

Public Function GenerateShapes(sFilename As String) As Boolean
'**********************************************************************************
    ' Date: 4/3/2001
    ' Abstract: Determines what type of file the user is attempting to open, and
    '           processes it accordingly.
    ' Assumptions:
    ' Notes:
'**********************************************************************************
    
    Dim oMapInfoParser As SpatialDataImport.MapInfoParser
    Dim oESRIParser As SpatialDataImport.ESRIParser
    Dim eFileType As FILE_TYPE
    
    On Error GoTo errorHandler
    
    'Get the File Type -- this will be used to determine what method we use to
    'parse the data.
    eFileType = GetFileType(sFilename)
    
    Select Case eFileType
        Case FILE_TYPE_MAPINFO_ASCII
            'This is a MapInfo ASCII Text file.
            
            'Create a new instance of the MapInfoParser object.
            Set oMapInfoParser = New SpatialDataImport.MapInfoParser
            
            'Generate shapes.  Note that the m_oObjectsToRender variable
            'is declared as Object, which allows us to pass either a DataPoint,
            'a Region, or a Line object.

            Set m_oObjectsToRender = oMapInfoParser.GenerateShapes(sFilename)
                        
            'If we got shapes back, then return True.  Otherwise return False.
            If m_oObjectsToRender Is Nothing Then
                GenerateShapes = False
            Else
                If m_oObjectsToRender.Count = 0 Then
                    GenerateShapes = False
                Else
                    GenerateShapes = True
                End If
            End If
            
        Case FILE_TYPE_MAPINFO_BINARY
        Case FILE_TYPE_ESRI_ASCII
        Case FILE_TYPE_ESRI_BINARY
            'This is a MapInfo ASCII Text file.
            
            'Create a new instance of the MapInfoParser object.
            Set oESRIParser = New SpatialDataImport.ESRIParser
            
            'Generate shapes.  Note that the m_oObjectsToRender variable
            'is declared as Object, which allows us to pass either a DataPoint,
            'a Region, or a Line object.
            Set m_oObjectsToRender = oESRIParser.GenerateShapes(sFilename)
            
            'If we got shapes back, then return True.  Otherwise return False.
            If m_oObjectsToRender Is Nothing Then
                GenerateShapes = False
            Else
                If m_oObjectsToRender.Count = 0 Then
                    GenerateShapes = False
                Else
                    GenerateShapes = True
                End If
            End If
    End Select
    
    Exit Function
errorHandler:
    MsgBox "Error in GenerateShapes" & vbCrLf _
         & "Error Number: " & Err.Number & vbCrLf _
         & "Error Description: " & Err.Description, vbExclamation, App.Title
End Function

Public Sub RenderShapes(omMap As MapPoint.Map)
'**********************************************************************************
    ' Date: 4/3/2001
    ' Abstract: Renders the generated shapes in MapPoint.
    ' Assumptions:
    ' Notes:
'**********************************************************************************
    
    On Error GoTo errorHandler
    
    'Make sure we actually have shapes to process, then call the RenderShapes method
    'on the collection object.
    If Not m_oObjectsToRender Is Nothing Then
        m_oObjectsToRender.RenderShapes omMap
    End If
    
    Exit Sub
errorHandler:
    MsgBox "Error in RenderShapes" & vbCrLf _
         & "Error Number: " & Err.Number & vbCrLf _
         & "Error Description: " & Err.Description, vbExclamation, App.Title
End Sub

Private Function GetFileType(sFilename As String) As FILE_TYPE
'**********************************************************************************
    ' Date: 4/3/2001
    ' Abstract: Determines the file type of the given file.
    ' Assumptions:
    ' Notes:
'**********************************************************************************
    
    Dim sFile As String
    Dim sExtension As String
    
    On Error GoTo errorHandler
    
    'For now, we'll rely on the file extension to tell us what type of file
    'we're dealing with.  In the future we may want to actually look at the
    'file to determine what type it is.
    
    ParseFileNameAndExtension sFilename, sFile, sExtension
    
    Select Case UCase$(sExtension)
        
        Case ".MIF"
            GetFileType = FILE_TYPE_MAPINFO_ASCII
        Case ".SHP"
            GetFileType = FILE_TYPE_ESRI_BINARY
        Case Else
            GetFileType = FILE_TYPE_UNKNOWN
    End Select
    
    Exit Function
errorHandler:
    MsgBox "Error in GetFileType" & vbCrLf _
         & "Error Number: " & Err.Number & vbCrLf _
         & "Error Description: " & Err.Description, vbExclamation, App.Title
End Function
